package com.example.cyh.bczapp.Code.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import com.example.cyh.bczapp.Code.BLL.UserBLL;
import com.example.cyh.bczapp.Code.Model.UserInfEntity;
import com.example.cyh.bczapp.R;

public class LoadActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);
    }
    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                        SharedPreferences pref =getSharedPreferences("Logindata",MODE_PRIVATE);
                        String id=pref.getString("id","");
                        String pwd=pref.getString("pwd","");
                        if(id!=""&&pwd!="")
                        {
                            UserBLL  ub=new UserBLL();
                            UserInfEntity user=ub.Login(id,pwd);
                            if(user!=null) {
                                Intent intent = new Intent(LoadActivity.this, MainActivity.class);
                                intent.putExtra("user", user);
                                LoadActivity.this.startActivity(intent);
                                finish();
                            }
                            else
                            {
                                Intent intent = new Intent(LoadActivity.this, LoginActivity.class);
                                LoadActivity.this.startActivity(intent);
                                finish();
                            }
                            Looper.prepare();
                            Toast.makeText(LoadActivity.this,user!=null?"登陆成功":"登陆失败", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                        else {
                        startActivity(new Intent(LoadActivity.this, LoginActivity.class));
                        finish();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
